/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tracker.entity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Workshop Class ID : SE0691 Student ID : SE90009 Student Name : Phạm Nguyễn
 * Nhật Trung Due Date : 12 November 2012 I declare that this assignment is my
 * own work in accordance with FPT Policy.
 */
@Stateless
public class TorrentsEntityFacade extends AbstractFacade<TorrentsEntity> {

    @PersistenceContext(unitName = "ASG2-Tracker1-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public TorrentsEntityFacade() {
        super(TorrentsEntity.class);
    }

    public List getTorrentsByName(String name) {
        Query query = em.createNamedQuery("TorrentsEntity.findByTorrentName");
        query.setParameter("torrentName", name);
        List list = query.getResultList();
        return list;
    }

    public boolean checkIsExistedTorrent(String name) {
        List list = getTorrentsByName(name);
        if (list != null && list.size() > 0) {
            return true;
        }
        return false;
    }

    public List findAllTorrentsByUserId(int userId) {
        Query query = em.createNamedQuery("TorrentsEntity.getAllTorrentsByUserId");
        query.setParameter("userId", userId);
        List list = query.getResultList();
        return list;
    }
    
    public void addTorrent(String torrentName, String torrentDescription, String torrentTime){
        TorrentsEntity entity = find(torrentName);
        if(entity != null){
            entity.setTorrentName(torrentName);
            entity.setTorrentTime(torrentTime);
            entity.setTorrentDescription(torrentDescription);
        }
    }

    public List findAllTorrentsByTorrentName(String torrentName) {
        List newLst = new ArrayList();
        try {
            List<TorrentsEntity> list = findAll();
            if (!list.isEmpty()) {
                Iterator<TorrentsEntity> it = list.iterator();
                while (it.hasNext()) {
                    TorrentsEntity entity = it.next();
                    if (entity != null) {
                        System.out.println("In Torrent: " + entity.getTorrentName().toLowerCase().contains(torrentName.toLowerCase()));
                        System.out.println("String: '" + torrentName + "'");
                        System.out.println("Entity: '" + entity.getTorrentName().trim().toLowerCase() + "'");
                        if (entity.getTorrentName().toLowerCase().contains(torrentName.toLowerCase())) {
                            newLst.add(entity);
                            System.out.println("Added in Torrents");
                        }
                    }

                }
            }
        } catch (Exception e) {
            System.out.println("Error when find Torrents by Name: " + e.getMessage());
        }

        return newLst;
    }
}
